<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="姓名" prop="studentName">
        <el-input
          v-model="queryParams.studentName"
          placeholder="请输入姓名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="学号" prop="studentNo">
        <el-input
          v-model="queryParams.studentNo"
          placeholder="请输入学号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="用户性别" prop="sex">
        <el-select v-model="queryParams.sex" placeholder="请选择用户性别" clearable>
          <el-option
            v-for="dict in dict.type.sys_user_sex"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="专业" prop="majorId">
        <el-select v-model="queryParams.majorId" placeholder="请选择专业" clearable>
          <el-option
            v-for="dict in majorList"
            :key="dict.majorId"
            :label="dict.majorName || ''"
            :value="parseInt(dict.majorId)"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="入学年级" prop="grade">
        <el-date-picker clearable
                        v-model="queryParams.grade"
                        type="date"
                        value-format="yyyy-MM-dd"
                        placeholder="请选择入学年级">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="班级" prop="classId">
        <el-select v-model="queryParams.classId" placeholder="请选择班级" clearable>
          <el-option
            v-for="dict in classList"
            :key="dict.classId"
            :label="dict.className || ''"
            :value="parseInt(dict.classId)"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="入学成绩" prop="admissionScore">
        <el-input
          v-model="queryParams.admissionScore"
          placeholder="请输入入学成绩"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="联系方式" prop="contact">
        <el-input
          v-model="queryParams.contact"
          placeholder="请输入联系方式"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="学籍状态" prop="studentStatus">
        <el-select v-model="queryParams.studentStatus" placeholder="请选择学籍状态" clearable>
          <el-option
            v-for="dict in dict.type.sys_student_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['core:students:add']"
        >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['core:students:edit']"
        >修改
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['core:students:remove']"
        >删除
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['core:students:export']"
        >导出
        </el-button>
        <el-col :span="1.5">
          <el-button
            type="success"
            plain
            size="mini"
            :disabled="multiple"
            @click="handleGenerateFeature"
          >生成特征值
          </el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button
            type="success"
            plain
            size="mini"
            :disabled="!(multiple && single)"
            @click="handleGeneratePredict"
          >预测结果
          </el-button>
        </el-col>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="studentsList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="学生ID" align="center" prop="studentId"/>
      <el-table-column label="学号" align="center" prop="studentNo"/>
      <el-table-column label="姓名" align="center" prop="studentName"/>
      <el-table-column label="用户性别" align="center" prop="sex">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_user_sex" :value="scope.row.sex"/>
        </template>
      </el-table-column>
      <el-table-column label="专业" align="center" prop="majorId">
        <template slot-scope="scope">
          {{ majorList.find(item => item.majorId === scope.row.majorId).majorName || '' }}
        </template>
      </el-table-column>
      <el-table-column label="入学年级" align="center" prop="grade" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.grade, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="班级" align="center" prop="classId">
        <template slot-scope="scope">
          {{ classList.find(item => item.classId === scope.row.classId).className || '' }}
        </template>
      </el-table-column>
      <el-table-column label="入学成绩" align="center" prop="admissionScore"/>
      <el-table-column label="联系方式" align="center" prop="contact"/>
      <el-table-column label="学籍状态" align="center" prop="studentStatus">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_student_status" :value="scope.row.studentStatus"/>
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="remark"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['core:students:edit']"
          >修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['core:students:remove']"
          >删除
          </el-button>
          <el-button
            size="mini"
            type="text"
            @click="handleGenerateFeature(scope.row)"
          >生成特征值
          </el-button>
          <el-button
            size="mini"
            type="text"
            @click="handleGeneratePredict(scope.row)"
          >预测结果
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改学生对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="学号" prop="studentNo">
          <el-input v-model="form.studentNo" placeholder="请输入学号"/>
        </el-form-item>
        <el-form-item label="姓名" prop="studentName">
          <el-input v-model="form.studentName" placeholder="请输入姓名"/>
        </el-form-item>
        <el-form-item label="用户性别" prop="sex">
          <el-radio-group v-model="form.sex">
            <el-radio
              v-for="dict in dict.type.sys_user_sex"
              :key="dict.value"
              :label="dict.value"
            >{{ dict.label }}
            </el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="专业" prop="majorId">
          <el-select v-model="form.majorId" placeholder="请选择专业">
            <el-option
              v-for="dict in majorList"
              :key="dict.majorId"
              :label="dict.majorName || ''"
              :value="parseInt(dict.majorId)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="入学年级" prop="grade">
          <el-date-picker clearable
                          v-model="form.grade"
                          type="year"
                          value-format="yyyy"
                          placeholder="请选择入学年级">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="班级" prop="classId">
          <el-select v-model="form.classId" placeholder="请选择班级">
            <el-option
              v-for="dict in classList"
              :key="dict.classId"
              :label="dict.className || ''"
              :value="parseInt(dict.classId)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="入学成绩" prop="admissionScore">
          <el-input v-model="form.admissionScore" placeholder="请输入入学成绩"/>
        </el-form-item>
        <el-form-item label="联系方式" prop="contact">
          <el-input v-model="form.contact" placeholder="请输入联系方式"/>
        </el-form-item>
        <el-form-item label="学籍状态" prop="studentStatus">
          <el-select v-model="form.studentStatus" placeholder="请选择学籍状态">
            <el-option
              v-for="dict in dict.type.sys_student_status"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 生成特征值 -->
    <el-dialog :title="titleFeature" :visible.sync="openFeature" width="500px" append-to-body>
      <el-form ref="featureForm" :model="featureParams" :rules="featureRules" label-width="80px">
        <el-form-item label="学期" prop="semesterId">
          <el-select v-model="featureParams.semesterId" placeholder="请选择学期">
            <el-option
              v-for="dict in semesterList"
              :key="dict.semesterId"
              :label="dict.semesterName || ''"
              :value="parseInt(dict.semesterId)"
            ></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitFeatureForm">确 定</el-button>
        <el-button @click="cancelFeature">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 预测结果 -->
    <el-dialog :title="titlePredict" :visible.sync="openPredict" width="500px" append-to-body>
      <el-form ref="predictForm" :model="predictParams" :rules="predictRules" label-width="80px">
        <el-form-item label="学期" prop="semesterId">
          <el-select v-model="predictParams.semesterId" placeholder="请选择学期">
            <el-option
              v-for="dict in semesterList"
              :key="dict.semesterId"
              :label="dict.semesterName || ''"
              :value="parseInt(dict.semesterId)"
            ></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitPredictForm">确 定</el-button>
        <el-button @click="cancelPredict">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listStudents,
  getStudents,
  delStudents,
  addStudents,
  updateStudents,
  generateFeatureBatch, generateFeature, predictSingle, predictBatch
} from "@/api/core/students"
import {listMajor} from "@/api/base/major";
import {listClass} from "@/api/base/class";
import {listSemester} from "@/api/base/semester";

export default {
  name: "Students",
  dicts: ['sys_job_status', 'sys_user_sex', 'sys_student_status', 'sys_normal_disable'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 学生表格数据
      studentsList: [],
      // 弹出层标题
      title: "",
      // 特征值弹出层标题
      titleFeature: "",
      // 预测弹出层标题
      titlePredict: "",
      // 是否显示弹出层
      open: false,
      // 是否显示特征值弹出层
      openFeature: false,
      // 是否显示预测弹出层
      openPredict: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        studentName: null,
        sex: null,
        majorId: null,
        grade: null,
        classId: null,
        admissionScore: null,
        contact: null,
        studentStatus: null,
        studentNo: null
      },
      // 表单参数
      form: {},
      // 特征值参数
      featureParams: {},
      // 预测参数
      predictParams: {},
      // 表单校验
      rules: {
        studentNo: [
          {required: true, message: "学号不能为空", trigger: "blur"}
        ],
        studentName: [
          {required: true, message: "姓名不能为空", trigger: "blur"}
        ],
        sex: [
          {required: true, message: "用户性别不能为空", trigger: "change"}
        ],
        majorId: [
          {required: true, message: "专业不能为空", trigger: "change"}
        ],
        grade: [
          {required: true, message: "入学年级不能为空", trigger: "blur"}
        ],
        classId: [
          {required: true, message: "班级不能为空", trigger: "change"}
        ],
        contact: [
          {required: true, message: "联系方式不能为空", trigger: "blur"}
        ],
        studentStatus: [
          {required: true, message: "学籍状态不能为空", trigger: "change"}
        ],
      },
      // 特征值表单校验
      featureRules: {
        semesterId: [
          {required: true, message: "学期不能为空", trigger: "change"}
        ],
      },
      // 预测表单校验
      predictRules: {
        semesterId: [
          {required: true, message: "学期不能为空", trigger: "change"}
        ]
      },
      majorList: [],
      classList: [],
      semesterList: []
    }
  },
  created() {
    this.getMajorList()
    this.getClassList()
    this.getSemesterList()
    this.getList()
  },
  methods: {
    /** 查询学生列表 */
    getList() {
      this.loading = true
      listStudents(this.queryParams).then(response => {
        this.studentsList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    getMajorList() {
      listMajor({pageNum: -1}).then(response => {
        this.majorList = response.rows
      })
    },
    getClassList() {
      listClass({pageNum: -1}).then(response => {
        this.classList = response.rows
      })
    },
    getSemesterList() {
      listSemester({pageNum: -1}).then(response => {
        this.semesterList = response.rows
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 取消特征值按钮
    cancelFeature() {
      this.openFeature = false
      this.resetFeature()
    },
    // 预测取消按钮
    cancelPredict() {
      this.openPredict = false
      this.resetPredict()
    },
    // 表单重置
    reset() {
      this.form = {
        studentId: null,
        studentName: null,
        sex: null,
        majorId: null,
        grade: null,
        classId: null,
        admissionScore: null,
        contact: null,
        studentStatus: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null
      }
      this.resetForm("form")
    },
    // 特征值表单重置
    resetFeature() {
      this.featureParams = {
        semesterId: null
      }
      this.resetForm("featureForm")
    },
    // 预测表单重置
    resetPredict() {
      this.predictParams = {
        semesterId: null
      }
      this.resetForm("predictForm")
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm")
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.studentId)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.open = true
      this.title = "添加学生"
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      const studentId = row.studentId || this.ids
      getStudents(studentId).then(response => {
        this.form = response.data
        this.open = true
        this.title = "修改学生"
      })
    },
    /** 生成特征值按钮操作 */
    handleGenerateFeature(row) {
      this.resetFeature();
      let studentId = row.studentId || this.ids
      this.featureParams.studentIds = studentId
      this.getSemesterList();
      this.openFeature = true;
      this.titleFeature = "生成特征值"
    },
    /** 生成预测按钮操作 */
    handleGeneratePredict(row) {
      this.resetPredict();
      let studentId = row.studentId || this.ids
      this.predictParams.studentIds = studentId
      this.getSemesterList();
      this.openPredict = true;
      this.titlePredict = "生成预测";
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.studentId != null) {
            updateStudents(this.form).then(response => {
              this.$modal.msgSuccess("修改成功")
              this.open = false
              this.getList()
            })
          } else {
            addStudents(this.form).then(response => {
              this.$modal.msgSuccess("新增成功")
              this.open = false
              this.getList()
            })
          }
        }
      })
    },
    /** 生成特征值提交按钮 */
    submitFeatureForm() {
      this.$refs["featureForm"].validate(valid => {
        if (valid) {
          let studentIdArr = this.featureParams.studentIds;
          let param = {};
          if (studentIdArr instanceof Array && studentIdArr.length > 0) {
            param.list = studentIdArr.map(item => {
              return {
                studentId: item,
                semesterId: this.featureParams.semesterId
              }
            })
            generateFeatureBatch(param).then(response => {
              this.$modal.msgSuccess("生成成功")
              this.openFeature = false
              this.getList()
            })
          } else {
            param.studentId = studentIdArr;
            param.semesterId = this.featureParams.semesterId;
            generateFeature(param).then(response => {
              this.$modal.msgSuccess("生成成功")
              this.openFeature = false
              this.getList()
            })
          }
        }
      })
    },
    /** 预测提交按钮 */
    submitPredictForm() {
      this.$refs["predictForm"].validate(valid => {
        if (valid) {
          let studentIdArr = this.predictParams.studentIds;
          console.log(studentIdArr.length)
          let param = {};
          if (studentIdArr instanceof Array && studentIdArr.length == 0) {
            param.semesterId = this.predictParams.semesterId;
            predictBatch(param).then(response => {
              this.$modal.msgSuccess("预测成功")
              this.openPredict = false
            })
          } else {
            param.studentId = studentIdArr;
            param.semesterId = this.predictParams.semesterId;
            predictSingle(param).then(response => {
              this.$modal.msgSuccess("预测成功")
              this.openPredict = false
            })
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const studentIds = row.studentId || this.ids
      this.$modal.confirm('是否确认删除学生编号为"' + studentIds + '"的数据项？').then(function () {
        return delStudents(studentIds)
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess("删除成功")
      }).catch(() => {
      })
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('core/students/export', {
        ...this.queryParams
      }, `students_${new Date().getTime()}.xlsx`)
    }
  }
}
</script>
